<?php

class KnowledgeController extends BaseArticleController {
	
	protected $layout = 'layouts.master';

    /**
     * 一周热文（阅读量最多)
     */
    public function getWeeklyHotArticles()
    {
    	$class_id = Input::get('class_id');
    	$filters = array(Article::TYPE_INFO, Article::TYPE_CASE, Article::TYPE_LIBRARY); // 资讯，案例，文库
    	if (in_array($class_id, $filters)) {
    		$filters = $class_id;
    	}
    	return $this->getArticleListView(
    		'general.200', 
    		$filters, 
    		$orderBy = 'hits',
    		$subDays = 7);
    }

    /**
     * 最新的律师发布的新闻
     */
    public function getLatestNewsItem()
    {
        $article = Article::getLatestArticleByLawyer();
        return $this->makeView( 'general.200', $article);
    }

    /**
     * 推荐 （点赞最多)
     */
    public function getMostLikedArticles()
    {
    	$class_id = Input::get('class_id');
    	$filters = array(Article::TYPE_INFO, Article::TYPE_CASE, Article::TYPE_LIBRARY); // 资讯，案例，文库
    	if (in_array($class_id, $filters)) {
    		$filters = $class_id;
    	}
    	return $this->getArticleListView(
    		'general.200', 
    		$filters, 
    		$orderBy = 'likes');
    }

    /**
     * 知识列表（包括了文库和法规） API endpoint to get all knowledge
     */
	public function listKnowledge() {
		return $this->getArticleListView(
			$viewName = 'knowledge.document',
			array(Article::TYPE_LAW, Article::TYPE_LIBRARY)
		);
	}

    /**
     * 看新闻
     */
    public function listContainer()
    {
        $this->layout->content = View::make('news.list')->with('class_id', Article::TYPE_LIBRARY);
    }

	/**
	 * 文书下载
	 */
	public function documentListContainer() {
        $workScopeList = UtilsHelper::documentCats();
        $data = [];
        $data['workscope'] = $workScopeList;
        //echo "<pre>";
        //var_dump($workScopeList);
        //die('a');
		$this->makeView( 'knowledge.document', $data);
	}

	/**
	 * 下载文书类别
	 */
	public function documentCategories() {
        $docCats = UtilsHelper::documentCats();
	    return $this->makeView( 'knowledge.document.categories', $docCats);
	}
	

	/**
	 * API endpoint to get all documents
	 */
	public function listDocument() {
		return $this->getArticleListView(
			$viewName = 'knowledge.document',
			$articleType = Article::TYPE_DOCUMENT
		);
	}

	/**
	 * API endpoint to get all articles
	 */
	public function listArticle() {
		return $this->getArticleListView(
			$viewName = 'knowledge.article',
			$articleType = Article::TYPE_LIBRARY
		);
	}

    public function viewLibraryArticle($id)
    {
        return $this->articleView($id, 'news.view', Article::TYPE_LIBRARY);
    }

    public function viewDocument($id)
    {
        return $this->articleView($id, 'news.view', Article::TYPE_DOCUMENT);
    }

    public function view($id)
    {
        return $this->articleView($id, 'news.view');
    }
    
    public function download($id)
    {
    	$article = Article::find($id);
    	if($article->class == Article::TYPE_DOCUMENT){
    		$article->increment('downs');
    		$article->save();
    		return $this->makeView('general.201', ['downs' => $article->downs], 201);
    	}
    }
	
}